package com.woniu.controller;

import com.woniu.pojo.Action;
import com.woniu.pojo.Discuss;
import com.woniu.pojo.PageBean;
import com.woniu.pojo.User;
import com.woniu.service.ActionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ActionController {

    @Resource
    private ActionService actionService;

    @RequestMapping("cal")
    public String cal(){
        return "cal";
    }

    /**
     *查询所有活动
     * @param
     * @return
     */
    @RequestMapping("actionDo")
    @ResponseBody
    public Map<String,Object> actionDo(PageBean pageBean, String apalce, String begin, String end) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Action> actionList = actionService.selectAll(pageBean,apalce,begin,end);
            map.put("actionList", actionList);
            map.put("result", true);
            map.put("pageBean",pageBean);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     *查询一个活动的详情
     * @param
     * @return
     */
    @RequestMapping("showDo")
    @ResponseBody
    public Map<String,Object> showDo(Integer aid) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Discuss> discussList = actionService.selectOne(aid);
            map.put("discussList", discussList);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     *是否点过赞判断
     * @param
     * @return
     */
    @RequestMapping("isgoodDo")
    @ResponseBody
    public Map<String,Object> isgoodDo(Integer aid,HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer i = actionService.isgood(aid,session);
            if(i > 0){
                map.put("result", "未点赞");
            }else {
                map.put("result", "已点赞");
            }
            map.put("aid",aid);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     *进行点赞
     * @param
     * @return
     */
    @RequestMapping("goodDo")
    @ResponseBody
    public Map<String,Object> goodDo(Integer aid, Integer good,HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer i = actionService.good(aid,good,session);
            if(i > 0){
                map.put("result", true);
                map.put("aid",aid);
            }
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }
    /**
     *取消点赞
     * @param
     * @return
     */
    @RequestMapping("badDo")
    @ResponseBody
    public Map<String,Object> badDo(Integer aid, Integer good,HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer i = actionService.bad(aid,good,session);
            if(i > 0){
                map.put("result", true);
                map.put("aid",aid);
            }else {
                map.put("result", false);
            }
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     *进行评论
     * @param
     * @return
     */
    @RequestMapping("submitDo")
    @ResponseBody
    public Map<String,Object> submitDo(Discuss discuss,HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer i = actionService.submit(discuss,session);
            if(i > 0){
                map.put("result", true);
                map.put("aid",discuss.getAid());
            }else {
                map.put("result", false);
            }
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 跳转到活动详情页面
     * @return
     */
    @RequestMapping("actioninfo")
    public String actioninfo(Integer aid,String apalce,String begin,String end, Model model){
        model.addAttribute("aid",aid);
        model.addAttribute("apalce",apalce);
        model.addAttribute("begin",begin);
        model.addAttribute("end",end);
        return "actioninfo";
    }
    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping("actioninfo2main")
    public String actioninfo2main(Integer aid,String apalce,String begin,String end, Model model){
        model.addAttribute("apalce",apalce);
        model.addAttribute("begin",begin);
        model.addAttribute("end",end);
        return "main";
    }
    /**
     * 是否为管理员验证
     */
    @RequestMapping("isManager")
    @ResponseBody
    public Map<String, Object> isManager(HttpSession session){
        Map<String, Object> map = new HashMap<>();
        try {
           Boolean fs = actionService.isManager(session);
            if(fs){
                map.put("result", true);
            }else {
                map.put("result", false);
            }
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 增加一个活动
     */
    @RequestMapping("saveDo")
    @ResponseBody
    public Map<String, Object> saveDo(HttpSession session,Action action){
        User user = (User)session.getAttribute("loginUser");
        action.setUid(user.getUid());
        Map<String, Object> map = new HashMap<>();
        try {
            int i = actionService.saveaction(action);
            if(i>0){
                map.put("result", true);
            }else {
                map.put("result", false);
            }
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

}
